package appx.craft.droid.uc;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;
import appx.craft.droid.R;
import appx.craft.droid.bean.CellBean;
import appx.craft.droid.bean.TableBean;


public class TableListViewAdapter extends BaseAdapter{
	
	private ArrayList<TableBean> mTempArrayList;
	private LayoutInflater mLayoutInflater;
	private ITableCellListener mCellCallBack;
	
	public TableListViewAdapter(Context vContext){
		mTempArrayList = new ArrayList<TableBean>();
		mLayoutInflater = LayoutInflater.from(vContext);
		mCellCallBack = (ITableCellListener)vContext;
	}
	
	public void addTableItem(TableBean v){
		if(mTempArrayList != null){
			mTempArrayList.add(v);
		}
	}
	
	public void addTableItems(ArrayList<TableBean> vTempTableBean){
		
		if(!mTempArrayList.isEmpty()){
			mTempArrayList.clear();
		}
		
		for(TableBean objTableBean : vTempTableBean){
			mTempArrayList.add(objTableBean);
		}
	}
	
	@Override
	public int getCount() {
		return mTempArrayList.size();
	}

	@Override
	public Object getItem(int position) {
		return mTempArrayList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		convertView = null;
		ContentHolder mContentHolder = null;
		TableBean mTableBean = mTempArrayList.get(position);
		if(convertView == null){
			
			mContentHolder = new ContentHolder();
			convertView = mLayoutInflater.inflate(R.layout.row_view_table_cell,null);
			mContentHolder.mTextView_Header = (TextView)convertView.findViewById(R.id.text_view_header_id);
			mContentHolder.mContainer_cells = (LinearLayout)convertView.findViewById(R.id.content_row_cells_id);
		}
		
		if(mTableBean != null){
			
			if(mContentHolder != null){
				
				// cell header 
				mContentHolder.mTextView_Header.setText(mTableBean.getCellHeader());
				
				for (int i = 0; i < mTableBean.getmArrayList_cellBean().size(); i++) {
					
					final CellBean mTempCellBean = mTableBean.getmArrayList_cellBean().get(i);
					
					if(i == 0){
						
						View mTopCellView = null;
						/** If size is one at that time **/
						if(mTableBean.getmArrayList_cellBean().size() == 1){
							mTopCellView = mLayoutInflater.inflate(R.layout.row_view_top_cell_vol_2,null);
						}else{
							mTopCellView = mLayoutInflater.inflate(R.layout.row_view_top_cell,null);
						}
						
						mTopCellView.setOnClickListener(new OnClickListener() {
							
							@Override
							public void onClick(View v) {
								mCellCallBack.onTableCellItemClick(Integer.parseInt(mTempCellBean.getRowID()),position);
							}
						});
						final TextView mCellItemText = (TextView)mTopCellView.findViewById(R.id.text_view_cell_id);
						mCellItemText.setText(mTempCellBean.getRowText());
						mContentHolder.mContainer_cells.addView(mTopCellView);
						
					}else if(i == (mTableBean.getmArrayList_cellBean().size()-1)){
						
						final View mBottomCellView = mLayoutInflater.inflate(R.layout.row_view_bottom_cell,null);
						mBottomCellView.setOnClickListener(new OnClickListener() {
							
							@Override
							public void onClick(View v) {
								mCellCallBack.onTableCellItemClick(Integer.parseInt(mTempCellBean.getRowID()),position);
							}
						});
						final TextView mCellItemText = (TextView)mBottomCellView.findViewById(R.id.text_view_cell_id);
						mCellItemText.setText(mTempCellBean.getRowText());
						mContentHolder.mContainer_cells.addView(mBottomCellView);
						
					}else{
						
						final View mMiddleCellView = mLayoutInflater.inflate(R.layout.row_view_middle_cell,null);
						mMiddleCellView.setOnClickListener(new OnClickListener() {
							
							@Override
							public void onClick(View v) {
								mCellCallBack.onTableCellItemClick(Integer.parseInt(mTempCellBean.getRowID()),position);
							}
						});
						final TextView mCellItemText = (TextView)mMiddleCellView.findViewById(R.id.text_view_cell_id);
						mCellItemText.setText(mTempCellBean.getRowText());
						mContentHolder.mContainer_cells.addView(mMiddleCellView);
					}
				}
			}
		}
		
		return convertView;
	}
	
	class ContentHolder{
		
		TextView mTextView_Header;
		LinearLayout mContainer_cells;
	}

}
